<?php
	/** sous fonctionnalité de modification des fonctions d'un membre
	 * @author Sebastian Mosser
	 */

class sfMemberFunctionsEdit extends aSubFeature {


	public function __construct() {
		  $this->_acl = new AccessControlList(); 
      $this->_acl->setGranted(3);
      $this->_acl->setGranted(7);
		}


	protected function run() {
		$action = PropRetriever::getProp($_POST,"action");
		switch($action) {
			case "delete" :
				return $this->deleteAFunction();
			case "edit" :
				return $this->editAFunction();
			case "add" :
				return $this->addAFunction();
			default:
				return $this->displayForm();
		}
		
	}
	
	private function addAFunction() {
		$memberId = PropRetriever::getProp($_POST,"memberId");
		$aMember = Member::read($memberId);
		
		$seasonId = PropRetriever::getProp($_POST,"seasonId");
		$functionId = PropRetriever::getProp($_POST,"clubFunctionId");
		
		$aMember->addFunction($seasonId,$functionId);
		$aMember->update();
		
		return $this->displayForm();
	}
	
	private function editAFunction() {
		$memberId = PropRetriever::getProp($_POST,"memberId");
		$aMember = Member::read($memberId);
		
		$seasonId = PropRetriever::getProp($_POST,"seasonId");
		$functionId = PropRetriever::getProp($_POST,"funcId");
		$aMember->delfunction($seasonId);
		$aMember->addFunction($seasonId,$functionId);
		$aMember->update();
		
		return $this->displayForm(True);
	}
	
	
	private function deleteAFunction() {
	  $memberId = PropRetriever::getProp($_POST,"memberId");
		$aMember = Member::read($memberId);
		
		$seasonId = PropRetriever::getProp($_POST,"seasonId");
		$aMember->delFunction($seasonId);
		$aMember->update();
		
		return $this->displayForm();
	}
	
	private function displayForm($edited = False) {
		$memberId = PropRetriever::getProp($_POST,"memberId");
		$aMember = Member::read($memberId);
		
		$returnButton = sfMemberEdit::getReturnButton($memberId);
		
		$editFunctionHeader = '<table class="resultset">';
		$editFunctionHeader .= '<tr> <th> Saison </th> <th> Fonction </th>';
		$editFunctionHeader .= '<th colspan="2"> Action </th>';
		
		$functions = $aMember->getExistantFunctions();
		$editFunctionsArray = "";
		foreach($functions as $k => $v) {
			// formulaire de modification
			$editFunctionsArray .= "<tr> <form method=\"post\">";
			$editFunctionsArray .= "<input type=\"hidden\" name=\"memberId\"";
			$editFunctionsArray .= " value=\"$memberId\" />";
			$editFunctionsArray .= "<input type=\"hidden\" name=\"action\"";
			$editFunctionsArray .= " value=\"edit\" />";
			$editFunctionsArray .= "<input type=\"hidden\" name=\"seasonId\"";
			$editFunctionsArray .= " value=\"$k\" />";
			$aSeason = Season::read($k);
			$editFunctionsArray .= "<td>" . $aSeason->displayShort() . "</td>";
			
			$funcList = ClubFunction::getAsList($v);
			$editFunctionsArray .= "<td><select name=\"funcId\" class=\"edit\">";
			$editFunctionsArray .= $funcList . "</select></td>";

			$editFunctionsArray .= "<td><input type=\"submit\" value=\"Modifier\" ";
			$editFunctionsArray .= " class=\"edit\" />";
			$editFunctionsArray .= "</td> </form>";
			
			// Formulaire de suppression
			$editFunctionsArray .= "<td><form method=\"post\">";
			$editFunctionsArray .= "<input type=\"hidden\" name=\"memberId\"";
			$editFunctionsArray .= " value=\"$memberId\" />";
			$editFunctionsArray .= "<input type=\"hidden\" name=\"action\"";
			$editFunctionsArray .= " value=\"delete\" />";
			$editFunctionsArray .= "<input type=\"hidden\" name=\"seasonId\"";
			$editFunctionsArray .= " value=\"$k\" />";
			$editFunctionsArray .= "<input type=\"submit\" value=\"Supprimer\" ";
			$editFunctionsArray .= " class=\"edit\" />";
			
			$editFunctionsArray .= " </form> </td> </tr>\n";
		}
		
		if ($editFunctionsArray == "")
			$editFunctionsForm = "<p> <span class=\"error\"> Aucune Fonction trouvée !</span> </p>";
		else {
		  $editFunctionsForm = $editFunctionHeader;
		  $editFunctionsForm .= $editFunctionsArray;
		  $editFunctionsForm .= "</table>";
		}
		
		$clubFunctionOptions = ClubFunction::getAsList(ClubFunction::$PLAYER);
		$seasonOptions = Season::getAsList(Login::seasonChoosen(False));
		
		$edit = "";
		if ($edited) {
			$edit = "<div align=\"center\"><p> <span class=\"error\">";
			$edit .= "Modification effectuée avec succès </span> </p> </div>";
		}
		$name = $aMember->getFirstName() . " " . $aMember->getLastName();
		
		$page = <<<EOS

<h1> Modification des fonctions de ${name} </h1>

<h2> Explications </h2>
<p> 
  <ul>
    <li> Pour ajoutez une nouvelle fonction, choisissez la saison concernée puis la fonction a ajouter. Cliquez sur <span class="error">[ Ajouter ] </span>pour effectuer l'ajout dans le système </li>
    <li> Pour modifier une fonction, selectionnez la nouvelle fonction puis cliquez sur <span class="error">[ Modifier ]</span> dans la ligne du tableau </li>
    <li> Pour supprimer une fonction, cliquez sur <span class="error">[ Supprimer ] </span> dans la ligne du tableau a supprimer </li>
  </ul>
  <br />
<div align="center">
$returnButton
</div>

<h2> Ajouter une fonction </h2>
<div align="center">
<form method="post" >
	<fieldset>
		<legend> Ajout d'une Fonction </legend>
		<br />
		
		<label> 
			<span> Fonction : </span>
			<select name="clubFunctionId" class="edit" />
			${clubFunctionOptions}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span> Saison : </span>
			<select name="seasonId" class="edit" />
			${seasonOptions}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		<input type="hidden" name="memberId" value="$memberId" />
		<input type="hidden" name="action" value="add" />
		<input type="submit" value="Ajouter" class="edit" />
		
		<br />  <br />
	</fieldset>
</form>
</div>

<h2> Modifier les fonctions existantes </h2>
${edit}
<div align="center">
${editFunctionsForm}
</div>
EOS;
		return $page;	
	}
	
}

?>